[][src]Crate ruspiro_console

Console abstraction

This crate provides a console abstraction to enable string output to a configurable output channel. It also provides the convinient macros (print! and println!) to output text that are usually not available in [no_std] environments. However this crate also provide macros to indicate the severity of the message that shall be printed. Those are info!, warn! and error!.

Dependencies

As this crate uses macros to provide formatted strings it depends on the alloc crate. When using this crate therefore a heap memory allocator has to be provided to successfully build and link. This could be a custom baremetal allocator as provided with the corresponding crate ruspiro_allocator.

Usage

As the console crate refers to functions and structures of the core::alloc crate the final binary need to be linked with a custom allocator. However, the ruspiro-console can bring the RusPiRo specific allocator if you activate the feature with_allocator like so:

[dependencies]
ruspiro-console = { version = "0.0.2", features = ["with_allocator"] }

To actually set an active output channel you need to provide a structure that implements the ConsoleImpl trait. This for excample is done in the Uart like so:

impl ConsoleImpl for Uart0 {
    fn putc(&self, c: char) {
        self.send_char(c);
    }
 
    fn puts(&self, s: &str) {
        self.send_string(s);
    }
}

If this trait has been implemented this structure can be used as actual console. To use it there should be the following code written at the earliest possible point in the main crate of the binary (e.g. the kernel)

use ruspiro_console::*;
use ruspiro_uart::*; // as we demonstrate with the Uart.
 
fn demo() {
    let mut uart = Uart::new(); // create a new uart struct
    if uart.initialize(250_000_000, 115_200).is_ok() { // initialize the Uart with fixed core rate and baud rate
        CONSOLE.take_for(|cons| cons.replace(uart)); // from this point CONSOLE takes ownership of Uart
        // uncommenting the following line will give compiler error as uart is moved
        // uart.send_string("I'm assigned to a console");
    }
 
    // if everything went fine uart should be assigned to the console for generic output
    println!("Console is ready and display's through uart");
}

Re-exports

pub extern crate alloc;
pub use macros::*;

Modules

macros

Convinient output macros to print formatted strings to the configured channel of the console

Macros

error

This macro prefixes the output with "E: -". Other than this it works like the std::println!

info

This macro prefixes the output with "I: -". Other than this it works like the std::println!

print

This macro works like the std::print! one.

println

This macro works like the std::println! one

warn

This macro prefixes the output with "W: -". Other than this it works like the std::println!

Structs

Console

The representation of the abstract console

Statics

CONSOLE

The Console singleton used by print! and println! macros

Traits

ConsoleImpl

Every "real" console need to implement this trait. Also the explicit Drop trait need to be implemented as the drop method of the implementing console will be called as soon as the actual console does release ownership of it

Functions

print

The base printing function hidden behind the print! and println! macro. This function fowards all calls to the generic console which puts the string to the assigned output channel.